/******************************************************************************/
//
//							 Software License Agreement
//
// The software supplied herewith by Microchip Technology Incorporated
// (the �Company�) for its PICmicro� Microcontroller is intended and
// supplied to you, the Company�s customer, for use solely and
// exclusively on Microchip PICmicro Microcontroller products. The
// software is owned by the Company and/or its supplier, and is
// protected under applicable copyright laws. All rights are reserved.
//  Any use in violation of the foregoing restrictions may subject the
// user to criminal sanctions under applicable laws, as well as to
// civil liability for the breach of the terms and conditions of this
// license.
//
// THIS SOFTWARE IS PROVIDED IN AN �AS IS� CONDITION. NO WARRANTIES,
// WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
// TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
// IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
/*****************************************************************************/
//	File:		Sensorless_Basic_ADC.c
//
// This program runs a sensored BLDC motor in a closed PI loop
// The basic circuit is as depicted in ANXXXX.
//
//	Written By:		Daniel Torres,
//					Microchip Technology Inc
//
//
// The following files should be included in the MPLAB project:
//
//		Sensorless_Basic_ADC.c		-- Main source code file
//		p30f3010.h		-- MPLAB-C30  dsPIC30F3010 processor header
//		p30f3010.gld	-- Linker script file
//
//
/******************************************************************************/
//
// Revision History
//
// 11/30/07 -- First Version Close Loop with BEMF sensing using the ADC
/*****************************************************************************/

#ifndef MC_H_
#define MC_H_

#include "p33FJ12MC202.h"
#include "hardware_settings.h"

#define CLOSELOOPMODE			// If defined then the compiler will add the PI controller for speed


//#define FCY  29491200			//FRC w/PLL x16
//FCY = 37000000
#define MILLISEC FCY/40000 //29491		//1 mSec delay constant
#define FPWM 20000				//20KHz PWM Freq

#define MAX_PWMticks 1024		// Maximum PWM pulses applied to motor for detecting initial rotor position
#define MAX_DUTY_CYCLE 1992 //1469     // 100% duty cycle P1DC1=P1DC2=P1DC3 = 1469
#define MIN_DUTY_CYCLE 98 //72		// 002% duty cycle P1DC1=P1DC2=P1DC3 = 73
#define POLEPAIRS	6			// number of pole pairs of the motor
#define INDEX		1			// Commutation base index
#define SPEEDMULT	255034 //188032		// Factor used to calculated speed
						// PWM TIMEBASE = FCY/2
						// TIMER3 TIMEBASE / FCY/256
						// SPEEDMULT = (TIMER3 TIMEBASE/PWM TIMEBASE) * MAX_DUTY_CYLE


//InitMCClock & InitMCStuff beinhalten alle MC-Inits
void InitMCClock(void);
void InitMCStuff(void);

//wird  von InitMCStuff aufgerufen
void InitMCPWM(void);
//wird  von InitMCStuff aufgerufen
void InitADC10(void);
//wird  von InitMCStuff aufgerufen
void InitTMR3(void);

void Commutate(void);
void SpeedPILoopController(void);
void DelayNmSec(unsigned int N);
void mcMain(void);

#endif /* MC_H_ */
